package com.daikit.commons.shared.bean;

/**
 * Time bean
 *
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkTime
{

	private Integer hours;
	private Integer minutes;
	private Integer seconds;

	private boolean clearSeconds = false;

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// CONSTRUCTOR
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Default constructor
	 */
	public DkTime()
	{
		// Nothing done by default
	}

	/**
	 * Default constructor
	 *
	 * @param clearSeconds
	 *           true to clear seconds
	 */
	public DkTime(final boolean clearSeconds)
	{
		this.clearSeconds = clearSeconds;
	}

	/**
	 * Constructor from time in seconds
	 *
	 * @param timeInSeconds
	 *           the time in seconds
	 * @param clearSeconds
	 */
	public DkTime(final Integer timeInSeconds, final boolean clearSeconds)
	{
		this(clearSeconds);
		setTime(timeInSeconds);
	}

	/**
	 * Constructor
	 *
	 * @param hours
	 *           number of hours
	 * @param minutes
	 *           number of minutes
	 * @param seconds
	 *           number of seconds
	 */
	public DkTime(final Integer hours, final Integer minutes, final Integer seconds)
	{
		this.hours = hours;
		this.minutes = minutes;
		this.seconds = seconds;
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Set time from time in seconds
	 *
	 * @param timeInSeconds
	 *           the time in seconds
	 */
	public void setTime(final Integer timeInSeconds)
	{
		Integer hours = null;
		Integer minutes = null;
		Integer seconds = null;
		if (timeInSeconds != null)
		{
			final int valueInt = timeInSeconds.intValue();
			hours = valueInt / (60 * 60);
			final int rest = valueInt - hours * 60 * 60;
			minutes = rest / 60;
			seconds = rest - minutes * 60;
		}
		setHours(hours);
		setMinutes(minutes);
		setSeconds(seconds);
	}

	/**
	 * @return the time in seconds
	 */
	public Integer getTime()
	{
		final int hourInSeconds = hours == null ? 0 : hours.intValue() * 60 * 60;
		final int minutesInSeconds = minutes == null ? 0 : minutes.intValue() * 60;
		final int secondsCalc = seconds == null || clearSeconds ? 0 : seconds.intValue();
		return hours == null && minutes == null && seconds == null ? null : hourInSeconds + minutesInSeconds + secondsCalc;
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// GETTERS / SETTERS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * @return the hours
	 */
	public Integer getHours()
	{
		return hours;
	}

	/**
	 * @param hours
	 *           the hours to set
	 */
	public void setHours(final Integer hours)
	{
		this.hours = hours;
	}

	/**
	 * @return the minutes
	 */
	public Integer getMinutes()
	{
		return minutes;
	}

	/**
	 * @param minutes
	 *           the minutes to set
	 */
	public void setMinutes(final Integer minutes)
	{
		this.minutes = minutes;
	}

	/**
	 * @return the seconds
	 */
	public Integer getSeconds()
	{
		return seconds;
	}

	/**
	 * @param seconds
	 *           the seconds to set
	 */
	public void setSeconds(final Integer seconds)
	{
		this.seconds = seconds;
	}
}
